package com.pdstudio.carrecom.ui.activity.more;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.http.RequestParams;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.app.AppContext;
import com.pdstudio.carrecom.app.UIHelper;
import com.pdstudio.carrecom.bean.LoginInfo;
import com.pdstudio.carrecom.bean.ResultInfo;
import com.pdstudio.carrecom.bean.SuggestList;
import com.pdstudio.carrecom.bean.SuggestList.SContent;
import com.pdstudio.carrecom.tools.StringUtils;
import com.pdstudio.carrecom.ui.activity.ActivityLogin;
import com.pdstudio.carrecom.ui.activity.ActivityMain;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 用户反馈
 * 类: ActivitySuggestion <br>
 * 描述: TODO <br>
 * 作者: xiangzhaoyu  <br>
 * 时间: 2015-5-21 下午4:38:58
 */
public class ActivitySuggestion extends Activity {
	
	CheckBox mSu1,mSu2,mSu3,mSu4;//意见复选框
	TextView mSu1t,mSu2t,mSu3t,mSu4t;//文字
	EditText mSu5;//意见框
	Button mSubmit;
	private Button mCallService;//呼叫服务中心
	
	private TextView txtTitle;
	private ImageView ivBack;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_more_suggestion);

		initTitle();
		InitialView();
	}
	
	/**
	 * 方法: initTitle <br>
	 * 描述: 初始化页面标题 <br>
	 * 作者: ws <br>
	 * 时间: 2015-5-24 下午5:46:11
	 */
	private void initTitle() {
		txtTitle = (TextView) findViewById(R.id.title_text_nav);

		txtTitle.setText("意见反馈");

		ivBack = (ImageView) findViewById(R.id.title_back);
		ivBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();				
			}
		});
	}

	private void InitialView(){
		mSu1=(CheckBox)findViewById(R.id.more_sug1);
		mSu2=(CheckBox)findViewById(R.id.more_sug2);
		mSu3=(CheckBox)findViewById(R.id.more_sug3);
		mSu4=(CheckBox)findViewById(R.id.more_sug4);
		mSu1t=(TextView)findViewById(R.id.more_sug1t);
		mSu2t=(TextView)findViewById(R.id.more_sug2t);
		mSu3t=(TextView)findViewById(R.id.more_sug3t);
		mSu4t=(TextView)findViewById(R.id.more_sug4t);
		mSu5=(EditText)findViewById(R.id.more_sug5);
		mSubmit=(Button)findViewById(R.id.more_submit);
		mSubmit.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(mSu5.getText()==null||mSu5.getText().toString().equals("")){
					UIHelper.ToastMessage(ActivitySuggestion.this, "请先填写提交意见！");
				}else{
					try {
						HttpExecuteJson http = new HttpExecuteJson(ActivitySuggestion.this, mSuggestionPostListener);
						RequestParams rps=new RequestParams();
						rps.addBodyParameter("uid", AppContext.getInstance()._userId+"");
						rps.addBodyParameter("os", "android");
						rps.addBodyParameter("content", StringUtils.utf8Encode(mSu5.getText().toString(),mSu5.getText().toString()));
						rps.addBodyParameter("type", "");
						mSubmit.setText("正在提交...");
						http.post(ServiceHelper.SuggestionPost, rps);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}});
		
		mCallService = (Button) findViewById(R.id.more_call);
		mCallService.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mCallService.setText("准备呼叫...");
				try {
					HttpExecuteJson http = new HttpExecuteJson(ActivitySuggestion.this, mCallMyExListener);
					Map<String, Object> rps = new HashMap<String, Object>();
					rps.put("id", AppContext.getInstance()._userName);
					http.get(ServiceHelper.CallMyEx, rps);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}});
		
		//try {
				//	HttpExecuteJson http = new HttpExecuteJson(this, mGuidePicsListener);
				//	Map<String, Object> rps = new HashMap<String, Object>();
				//	http.get(ServiceHelper.GuidePage, rps);
				//} catch (Exception e) {
				//	e.printStackTrace();
				//}
				
				//测试数据
				String json="{\"msg\":\"fail\",\"ret\":\"-1\"}";
				json="{\"msg\":\"success\",\"ret\":\"0\",\"suggestionList\":[{\"id\":\"1\",\"content\":\"1\"},{\"id\":\"2\",\"content\":\"2\"},{\"id\":\"3\",\"content\":\"3\"}]}";
				DoJson(json);
	}
	
	private void DoJson(String json){
		SuggestList edv = null;
		try {
			edv = new Gson().fromJson(json,
					new TypeToken<SuggestList>() {
					}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				//原本在initview的代码放在这下面
				try{
					List<SContent> temps1 = new ArrayList<SContent>();
					if(edv.suggestionList!=null){
						for(int i=0;i<edv.suggestionList.size();i++){
							if(edv.suggestionList.get(i)!=null&&(edv.suggestionList.get(i).content!=null&&edv.suggestionList.get(i).content!="")){
								temps1.add(edv.suggestionList.get(i));
							}
						}
						try{
							mSu1.setText(temps1.get(0).id);
							mSu1t.setText(temps1.get(0).content);
							mSu2.setText(temps1.get(1).id);
							mSu2t.setText(temps1.get(1).content);
							mSu3.setText(temps1.get(2).id);
							mSu3t.setText(temps1.get(2).content);
							mSu4.setText(temps1.get(3).id);
							mSu4t.setText(temps1.get(3).content);
						}catch(Exception e){e.printStackTrace();}
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	private httpReturnJson mSuggestionlListener = new httpReturnJson() {
		@Override
		public void onFailure(int error, String msg) {
			UIHelper.ToastMessage(ActivitySuggestion.this, "获取意见列表失败" + msg);
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(ActivitySuggestion.this, "获取意见列表退出");
		}

		// 获取到饲料配方类型成功
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			// UIHelper.ToastMessage(_context,"总条数=" + result + "");
			DoJson(result);
		}
	};
	
	private void DoJsonSuggestionPost(String json){
		LoginInfo edv = null;
		try {
			edv = new Gson().fromJson(json,
					new TypeToken<LoginInfo>() {
					}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				//原本在initview的代码放在这下面
					UIHelper.ToastMessage(ActivitySuggestion.this, "提交成功!");
					finish();
				}else{
					UIHelper.ToastMessage(ActivitySuggestion.this, "提交失败!"+edv.msg);
				
			}
			mSubmit.setText("提交");
		} catch (Exception e) {
			e.printStackTrace();
			UIHelper.ToastMessage(ActivitySuggestion.this, "提交失败!"+e.getMessage());
		}

	}
	
	private httpReturnJson mSuggestionPostListener = new httpReturnJson() {
		@Override
		public void onFailure(int error, String msg) {
			UIHelper.ToastMessage(ActivitySuggestion.this, "提交失败" + msg);
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(ActivitySuggestion.this, "提交失败");
		}

		// 获取到饲料配方类型成功
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			// UIHelper.ToastMessage(_context,"总条数=" + result + "");
			DoJsonSuggestionPost(result);
		}
	};
	
	/*
	 * 呼叫专属客服*/
	private void DoJsonCallMyEx(String json){
		mCallService.setText("呼叫我的专属顾问");
		ResultInfo edv = null;
		try {
			edv = new Gson().fromJson(json,
					new TypeToken<ResultInfo>() {
					}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				//原本在initview的代码放在这下面
				try{
					if(edv.data!=null&&!edv.data.toString().equals("")){
						Intent intent=new Intent();

						intent.setAction("android.intent.action.CALL");

						intent.setData(Uri.parse("tel:"+new Gson().toJson(edv.data)));

						startActivity(intent);

					}else{
						UIHelper.ToastMessage(ActivitySuggestion.this, "呼叫失败" );
					}
				}catch(Exception e){
					e.printStackTrace();
					UIHelper.ToastMessage(ActivitySuggestion.this, "呼叫失败" );
				}
			}else{
				UIHelper.ToastMessage(ActivitySuggestion.this, "呼叫失败" );
			}
		} catch (Exception e) {
			e.printStackTrace();
			UIHelper.ToastMessage(ActivitySuggestion.this, "呼叫失败" );
		}

	}
	
	private httpReturnJson mCallMyExListener = new httpReturnJson() {
		@Override
		public void onFailure(int error, String msg) {
			UIHelper.ToastMessage(ActivitySuggestion.this, "呼叫失败" + msg);
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(ActivitySuggestion.this, "呼叫失败");
		}

		// 获取到饲料配方类型成功
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			// UIHelper.ToastMessage(_context,"总条数=" + result + "");
			DoJsonCallMyEx(result);
		}
	};




}
